package com.taptech.doufu.ui.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.taptech.doufu.R;
import com.taptech.doufu.bean.CPLibBean;

import java.util.List;



/**
 * cp libs bean
 */
public class CpLibsAdapter extends RecyclerView.Adapter<CpLibsAdapter.ContactsViewHolder> {

    private List<CPLibBean> contacts;
    private int layoutId;
    private Context mContext;



    public CpLibsAdapter(List<CPLibBean> contacts, int layoutId, Context context) {
        this.contacts = contacts;
        this.layoutId = layoutId;
        this.mContext = context;
    }

    @Override
    public ContactsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(layoutId, null);
        return new ContactsViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ContactsViewHolder holder, final int position) {
        CPLibBean contact = contacts.get(position);
        if (position == 0 || !contacts.get(position-1).getIndex().equals(contact.getIndex())) {
            holder.tvIndex.setVisibility(View.VISIBLE);
            holder.item_tv_index_line.setVisibility(View.VISIBLE);
            holder.tvIndex.setText(contact.getIndex());
        } else {
            holder.tvIndex.setVisibility(View.GONE);
            holder.item_tv_index_line.setVisibility(View.GONE);
        }
        holder.item_name.setText(contact.getItem_cp_lib_name());
        if (contact.getItem_cp_lib_img() != null) {
            Picasso.with(mContext).load(contact.getItem_cp_lib_img()).error(mContext.getResources().getDrawable(R.drawable.cp_deaf_iv)).into(holder.item_img);
        }
        holder.item_count.setText(String.valueOf(contact.getItem_cp_lib_count()));
        holder.item_flower_count.setText(String.valueOf(contact.getItem_cp_lib_flower_count()));
    }

    @Override
    public int getItemCount() {
        return contacts.size();
    }

    class ContactsViewHolder extends RecyclerView.ViewHolder {
        private TextView tvIndex;
        private ImageView item_img;
        private TextView item_name;
        private TextView item_count;
        private TextView item_flower_count;
        private View item_tv_index_line;

        public ContactsViewHolder(View itemView) {
            super(itemView);
            tvIndex = (TextView) itemView.findViewById(R.id.tv_index);
            item_name = (TextView) itemView.findViewById(R.id.item_cp_lib_name);
            item_img = (ImageView) itemView.findViewById(R.id.item_cp_lib_img);
            item_count = (TextView) itemView.findViewById(R.id.item_cp_lib_count);
            item_flower_count = (TextView) itemView.findViewById(R.id.item_cp_lib_flower_count);
            item_tv_index_line = itemView.findViewById(R.id.tv_index_line);

        }
    }


}
